package com.jdd.modules.parkmonthly.util;

import com.jdd.common.util.excel.entity.enums.ExcelType;
import com.jdd.common.util.excel.entity.params.ExportParams;
import com.jdd.common.util.excel.entity.params.ImportParams;
import com.jdd.common.util.excel.util.ExcelExportUtil;
import com.jdd.common.util.excel.util.ExcelImportUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;


/**
 * @Description: Excel导入导出工具类
 * @author:
 * @date: 2022/3/1 16:24
 */
@Slf4j
public class ExcelUtils {
    public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName,
                                   boolean isCreateHeader, HttpServletResponse response, HttpServletRequest request) {
        ExportParams exportParams = new ExportParams(title, sheetName);
        exportParams.setCreateHeadRows(isCreateHeader);
        defaultExport(list, pojoClass, fileName, response, exportParams, request);
    }

    public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName,
                                   HttpServletResponse response, HttpServletRequest request) {
        defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName).
                setHeaderColor(HSSFColor.HSSFColorPredefined.RED.getIndex()), request);
    }

    public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response, HttpServletRequest request) {
        defaultExport(list, fileName, response, request);
    }

    private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response,
                                      ExportParams exportParams, HttpServletRequest request) {
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);
        if (workbook != null) {
            ;
        }
        downLoadExcel(fileName, response, workbook, request);
    }

    public static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook, HttpServletRequest request) {
        // 输出流
        OutputStream out = null;
        try {
            response.setContentType("application/vnd.ms-excel; charset=utf-8");
            String userAgent = request.getHeader("USER-AGENT");
            String finalFileName = null;
            if (StringUtils.contains(userAgent, "MSIE")) {
                //IE浏览器
                finalFileName = URLEncoder.encode(fileName, "UTF8");
            } else if (StringUtils.contains(userAgent, "Firefox") || StringUtils.contains(userAgent, "Chrome")) {
                //google,火狐浏览器
                if (StringUtils.contains(userAgent, "Edge")) {
                    finalFileName = URLEncoder.encode(fileName, "UTF8");
                } else {
                    finalFileName = new String(fileName.getBytes(), "ISO8859-1");
                }
            } else {
                //其他浏览器
                finalFileName = URLEncoder.encode(fileName, "UTF8");
            }
            response.setHeader("Content-Disposition", "attachment;filename=\"" + finalFileName + ".xls\"");
            response.setCharacterEncoding("utf-8");
            out = response.getOutputStream();
            workbook.write(out);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response, HttpServletRequest request) {
        Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);
        if (workbook != null) {
            ;
        }
        downLoadExcel(fileName, response, workbook, request);
    }

    public static <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> pojoClass) {
        if (StringUtils.isBlank(filePath)) {
            return null;
        }
        ImportParams params = new ImportParams();
        params.setTitleRows(titleRows);
        params.setHeadRows(headerRows);
        List<T> list = null;
        try {
            list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
        } catch (NoSuchElementException e) {
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows,
                                          Class<T> pojoClass) {
        if (file == null) {
            return null;
        }
        ImportParams params = new ImportParams();
        params.setTitleRows(titleRows);
        params.setHeadRows(headerRows);
        List<T> list = null;
        try {
            list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
        } catch (NoSuchElementException e) {
        } catch (Exception e) {
            log.info(e.getMessage());
        }
        return list;
    }

}
